<template>
  <InfoSphereSkeleton v-if="loading" :show="loading"/>
  <div v-else class="space-y-3 space-x-3">
    <UserPageable :items="items" :pagination="pagination" @changePage="changePage" @change="initialize"/>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import InfoSphereSkeleton from '@/views/components/skeleton/InfoSphereSkeleton.vue'
import { Book } from '@/model/book.ts'
import { Pagination } from '@/model/response.ts'
import UserService from '@/service/user.ts'
import UserPageable from '@/views/components/pageable/UserPageable.vue'

export default defineComponent({
  name: 'FansHome',
  components: { UserPageable, InfoSphereSkeleton },
  data()
  {
    return {
      loading: false,
      items: [] as Array<Book>,
      pagination: null as unknown as Pagination
    }
  },
  created()
  {
    this.pagination = { page: 1, size: 63 }
    this.initialize()
  },
  methods: {
    initialize()
    {
      const username = this.$route.params.username
      if (username) {
        this.loading = true
        UserService.getByUsernameAndFans(username as string, this.pagination)
                   .then(response => {
                     this.items = response.data.content
                     this.pagination = response.data.page
                   })
                   .finally(() => this.loading = false)
      }
    },
    changePage(value: Pagination)
    {
      this.pagination = value
      this.initialize()
    }
  }
})
</script>
